package org.fuys.etp.boot.starter;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.fuys.etp.core.*;
import org.fuys.etp.core.checker.EtpUpdateChecker;
import org.fuys.etp.core.checker.impl.LevelAlertChecker;
import org.fuys.etp.core.checker.impl.SystemResourceChecker;
import org.fuys.etp.core.checker.impl.ThresholdChecker;
import org.fuys.etp.core.metrics.EtpMetricsHandler;
import org.fuys.etp.core.metrics.impl.info.BaseInfoHandler;
import org.fuys.etp.core.metrics.impl.trend.AdaptiveTrendHandler;
import org.fuys.etp.core.metrics.impl.trend.StageSwitchTrendHandler;
import org.fuys.etp.core.metrics.impl.trend.type.StageJudgeTypeProcessor;
import org.fuys.etp.core.metrics.impl.trend.type.impl.BlockQueueRatioHandler;
import org.fuys.etp.core.metrics.impl.trend.type.impl.RejectCountHandler;
import org.fuys.etp.core.metrics.impl.trend.type.impl.TaskAvgTimeHandler;
import org.fuys.etp.core.reject.EtpRejectHandler;
import org.fuys.etp.core.reject.impl.NoticeRejectHandler;
import org.fuys.etp.external.alert.IAlertService;
import org.fuys.etp.external.alert.impl.LogAlertServiceImpl;
import org.fuys.etp.external.resource.ISystemInfo;
import org.fuys.etp.external.resource.impl.OshiSystemInfo;
import org.fuys.etp.registry.IEtpRegistry;
import org.fuys.etp.registry.impl.RedisEtpRegistry;
import org.fuys.etp.registry.impl.ZookeeperEtpRegistry;
import org.fuys.etp.repo.EtpRepoService;
import org.fuys.etp.repo.repository.IEtpConfigRepository;
import org.fuys.etp.repo.repository.IEtpStrategyRepository;
import org.fuys.etp.repo.repository.IEtpUpdateHistoryRepository;
import org.fuys.etp.repo.repository.impl.EtpConfigRepository;
import org.fuys.etp.repo.repository.impl.EtpStrategyRepository;
import org.fuys.etp.repo.repository.impl.EtpUpdateHistoryRepository;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @projectName: fuys-etp-boot-starter
 * @package: org.fuys.etp.boot.starter
 * @className: FuysEtpBootAutoConfig
 * @author: WangJing
 * @description: TODO
 * @date: 2025/2/24 12:27
 * @version: 1.0
 */

@Configuration
@MapperScans({@MapperScan(basePackages = "org.fuys.etp.repo.mapper")})
@EnableConfigurationProperties(value = {EtpPersistentConfig.class,EtpRegistryConfig.class})
@Configurable
public class FuysEtpBootAutoConfig {

    @Bean
    public EtpRejectHandler noticeRejectHandler(){
        return new NoticeRejectHandler();
    }

    @Bean
    public EtpMetricsHandler baseInfoHandler(){
        return new BaseInfoHandler();
    }

    @Bean
    public EtpMetricsHandler adaptiveTrendHandler(){
        return new AdaptiveTrendHandler();
    }

    @Bean
    public StageJudgeTypeProcessor blockQueueRatioHandle(){
        return new BlockQueueRatioHandler();
    }

    @Bean
    public StageJudgeTypeProcessor rejectCountHandle(){
        return new RejectCountHandler();
    }

    @Bean
    public StageJudgeTypeProcessor taskAvgTimeHandle(){
        return new TaskAvgTimeHandler();
    }

    @Bean
    public EtpUpdateChecker levelAlertChecker(){
        return new LevelAlertChecker();
    }

    @Bean
    public EtpUpdateChecker systemResourceChecker(){
        return new SystemResourceChecker();
    }

    @Bean
    public EtpUpdateChecker thresholdAlertChecker(){
        return new ThresholdChecker();
    }

    @Bean
    public ISystemInfo oshiSystemInfo(){
        return new OshiSystemInfo();
    }

    @Bean
    public IAlertService etpLogAlert(){
        return new LogAlertServiceImpl();
    }


    @Bean
    public EtpMetricsHandler stageSwitchTrendHandler(ListableBeanFactory beanFactory){
        return new StageSwitchTrendHandler(beanFactory);
    }

    @Bean
    public EtpGlobalConfig etpGlobalConfig(){
        return new EtpGlobalConfig();
    }


    @Bean
    public IEtpConfigRepository etpConfigRepository(){
        return new EtpConfigRepository();
    }

    @Bean
    public IEtpUpdateHistoryRepository etpUpdateHistoryRepository(){
        return new EtpUpdateHistoryRepository();
    }

    @Bean
    public IEtpStrategyRepository etpStrategyRepository(){
        return new EtpStrategyRepository();
    }

    @Bean
    public EtpRepoService etpRepoService(){
        return new EtpRepoService();
    }

    @Bean
    public EtpManager etpManager(EtpPersistentConfig globalConfig, EtpRepoService repoService,
                                 ListableBeanFactory beanFactory, ApplicationEventPublisher eventPublisher){
        return new EtpManager(globalConfig,repoService,beanFactory,eventPublisher);
    }

    @Bean
    public EtpDispatcher etpDispatcher(ListableBeanFactory beanFactory){
        return new EtpDispatcher(beanFactory);
    }

    @Bean
    public EtpCollector etpCollector(){
        return new EtpCollector();
    }

    @Bean
    @ConditionalOnProperty(prefix = "etp.config.registry",name = "type",havingValue = "redis")
    public IEtpRegistry redisRegistry(){
        return new RedisEtpRegistry();
    }

    @Bean
    @ConditionalOnProperty(prefix = "etp.config.registry",name = "type",havingValue = "zookeeper")
    public IEtpRegistry zookeeperRegistry(){
        return new ZookeeperEtpRegistry();
    }

}
